<template>

  <div class="login-container">
    <el-card class="box-card">
      <div class="header">
        <img src="@/assets/logo.png" alt="">
      </div>
      <div>
        <el-form :model="ruleForm" status-icon ref="ruleForm" :rules="formRule">
          <el-form-item label="用户名" prop="userName">
            <el-input v-model.number="ruleForm.userName" prefix-icon="el-icon-search"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"
              prefix-icon="el-icon-search"></el-input>
          </el-form-item>


          <el-form-item class="bottom">
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { login } from "@/api/index.js";
import { setToken, getToken } from "@/util/SetTime.js";
export default {
  name: "LoginPage",
  data() {
    return {
      ruleForm: {
        pass: '22',
        userName: '11',
        age: ''
      },
      formRule: {
        userName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        pass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]

      }
    };
  },
  methods: {
    submitForm() {

      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          login(this.ruleForm).then(async (res) => {
            console.log("登录成功", res.data.data.token)
            setToken("token", res.data.data.token);
            console.log('Token已设置:', getToken('token')) // 验证
            this.$router.push("/home")
          })


        }


      })
    },
    resetForm() {
      console.log(this);
      this.$refs.ruleForm.resetFields();
    }
  }
}
</script>

<style scoped lang="less">
/* 容器样式 - 使用 Flexbox 垂直居中所有内容 */
.login-container {
  width: 30%;

  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-30%, -50%);


  .header {
    width: 40px;
    height: 40px;
    border: 1px solid pink;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px;

    position: absolute;
    left: 50%;

    transform: translate(-50%, -70%);

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;

    }


  }


  .bottom {
    display: flex;
    justify-content: flex-end;
  }

}
</style>